<template>
  <view class="page-main">
    <slide-questions
      :dataList="info.questions"
      @submit="subData"
      :title="info.title"
    ></slide-questions>
  </view>
</template>

<script>
import slideQuestions from "../../components/slideQuestions/index.vue";
import { examDetail, submitPaper } from "../../api/index";
export default {
  components: {
    slideQuestions,
  },
  data() {
    return {
      id: "",
      info: {},
      time: "",
      list: [
        {
          id: "1",
          title: "这是一道单选题",
          problemType: "SINGLE",
          img: "https://imgs.699pic.com/images/401/760/322.jpg!list2x.v1",
          score: 5, //分值
          children: [
            {
              alias: "A",
              answer: "选项1",
              isSelect: 0, //这个是用来回显的字段(0:未选中；1:选中)(可不传)
            },
            {
              alias: "B",
              answer: "选项2",
              isSelect: 0, //这个是用来回显的字段(0:未选中；1:选中)(可不传)
            },
            {
              alias: "C",
              answer: "选项3",
              isSelect: 0, //这个是用来回显的字段(0:未选中；1:选中)(可不传)
            },
          ],
        },
        {
          id: "2",
          title: "这是一道多选题",
          problemType: "MULTY",
          score: 5, //分值
          children: [
            {
              alias: "A",
              answer: "选项1",
              isSelect: 0, //这个是用来回显的字段(0:未选中；1:选中)(可不传)
            },
            {
              alias: "B",
              answer: "选项2",
              isSelect: 0, //这个是用来回显的字段(0:未选中；1:选中)(可不传)
            },
            {
              alias: "C",
              answer: "选项3",
              isSelect: 0, //这个是用来回显的字段(0:未选中；1:选中)(可不传)
            },
            {
              alias: "D",
              answer: "选项4",
              isSelect: 0, //这个是用来回显的字段(0:未选中；1:选中)(可不传)
            },
          ],
        },
        {
          id: "3",
          score: 10, //分值
          title: "这是一道简答题",
          problemType: "QUESTION",
          userAnswer: "", //这个是用来回显的字段(可不传)
        },
      ], //问题列表数据
    };
  },

  onLoad(e) {
    console.log(e, "eeeee");
    this.getDetail(e.id);
    this.id = e.id;
    this.time = e.time;
    wx.showShareMenu({
      withShareTicket: true,
      menus: ["shareAppMessage", "shareTimeline"],
    });
  },
  methods: {
    // 获取试卷题目
    getDetail(id) {
      let params = {
        paperId: id,
      };
      examDetail(params).then((res) => {
        console.log(res, "sadsdasdas");
        this.info = res.data;
        this.info.questions.forEach((element) => {
          element.choices.forEach((i) => {
            i.isSelect = 0;
          });
        });
      });
    },
    formatDate(date) {
      const year = date.getFullYear();
      const month = ("0" + (date.getMonth() + 1)).slice(-2);
      const day = ("0" + date.getDate()).slice(-2);
      const hours = ("0" + date.getHours()).slice(-2);
      const minutes = ("0" + date.getMinutes()).slice(-2);
      const seconds = ("0" + date.getSeconds()).slice(-2);
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
    getMinutesBetween(startTime, endTime) {
      const start = new Date(startTime);
      const end = new Date(endTime);
      const diff = end - start;
      return Math.round(diff / (1000 * 60));
    },
    //点击提交
    subData(data) {
      const currentTime = this.formatDate(new Date());
      console.log(this.time, currentTime);
      let time = this.getMinutesBetween(this.time, currentTime);
      let parms = {
        spendTime: time,
        answers: data,
        endTime: currentTime,
        paperId: this.id,
        startTime: this.time,
      };
      submitPaper(parms).then((res) => {
        console.log(res, "sadsdasd");
        uni.navigateTo({
          url: "/pages/authentication/result?id=" + this.id,
        });
      });
    },
  },
};
</script>
